from base import Session
import redis
import json
SECTION = 'redis_session'
class RedisSession(Session):
    def config(self, cfg):
        self.cfg = cfg
        self.host = cfg.get(SECTION, 'host')
        self.port = int(cfg.get(SECTION, 'port'))
        self.db = int(cfg.get(SECTION, 'db'))
        
        self.redis = redis.Redis(host=self.host, port=self.port, db=self.db)

        
    def build_session(self):

        self.cacheid = "session" + self.sid
        
        js = self.redis.get(self.cacheid);

        if js != None:
            self.stores = json.loads(js)
        
    def commit(self):
        js = json.dumps(self.stores)

        self.redis.set(self.cacheid, js)